package com.soup.lab.spring.databind.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("modelAttr")
public class ModelAttributeController {

    @GetMapping("simpleGet")
    public SimpleModel simpleGet(SimpleModel model) {
        return model;
    }

    @PostMapping("simplePost")
    public SimpleModel simplePost(SimpleModel model) {
        return model;
    }

    @GetMapping("simpleMultiGet")
    public List<SimpleModel> simpleGet(SimpleModel model, @ModelAttribute(value = "defModel", binding = false) SimpleModel simpleModel) {
        return Arrays.asList(model, simpleModel);
    }

    @GetMapping("defModel")
    public SimpleModel defGet(@ModelAttribute(value = "defModel", binding = false) SimpleModel simpleModel) {
        return simpleModel;
    }

    @PostMapping("fileModel")
    public String defGet(FileModel fileModel) {
        System.out.println(fileModel);
        return "abc";
    }

//    @ModelAttribute("defModel")
//    public SimpleModel init() {
//        return new SimpleModel("a", 110l);
//    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SimpleModel {
        private String name;
        private Long age;
    }

//    @InitBinder
//    public void initBinder(WebDataBinder webDataBinder) {
//        webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
//    }


    @Data
    public static class FileModel {
        private MultipartFile file;
        private String name;
    }

}
